package org.one.stone.soup.grfxML;

import org.one.stone.soup.mjdb.data.field.LockException;
  /*
 * Copyright 2000-2001 Wet-Wired.com Ltd.,
 * Portsmouth England
 * All rights reserved.
 */

/**
	*
	* @author Nik Cross
*/
public class GrfxMLPrototypeBuilder {
protected static void buildPrototype(GrfxMLEngine engine,DataNode target, String prototype,DataNode parent)
{
	DataNode protoNode = findPrototype(engine,prototype);

	buildPrototype(engine,target,protoNode,parent,true);
}

protected static void buildPrototype(GrfxMLEngine engine,DataNode target, DataNode prototype,DataNode parent,boolean markAsPrototype)
{
	if(prototype==null)
	{
		GrfxMLException.warning( GrfxMLException.WARNING_BAD_PROTOTYPE_CODE ,new Object[]{prototype,target.getId()} );
		return;
	}

	String name = target.getId();
	DataNode instance = prototype.getClone( target.getId() );

	try{
		target.setId( name,instance );
	}
	catch(LockException le){}//dummy catch

	if(markAsPrototype)
	{
		target._prototype = prototype.getId();
	}
	target.translation = instance.translation;
	target.behaviour = instance.behaviour;
	target.shape = instance.shape;
	target.children = instance.children;
	target.process = instance.process;
	target.anchors = instance.anchors;
	target.setParent( parent );

	if(GrfxMLEngine.DEBUG) System.out.println("Prototype "+prototype+" built as "+target.getId());

	return;
}

protected static DataNode findPrototype(GrfxMLEngine engine,String prototype)
{
	DataChildren prototypes = engine.BASE.PROTOTYPES;
	return (DataNode) prototypes.findId(prototype);
}
}
